import { PopCommon2 } from "../Home/Popup/PopCommon2";
import { StaticData } from "../Data/StaticData";
import { GameRes } from "../GameRes";
import { EventNames } from "../Home/Data/EventNames";
import HttpAPI from "../HttpManager/HttpAPI";
import HttpManager from "../HttpManager/HttpManager";
import { WXLogin } from "../Tools/WXLogin";
import { WXLoginSceneBase } from "./WXLoginScene.generated";

const { regClass } = Laya;

@regClass()
export class WXLoginScene extends WXLoginSceneBase {

    //组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
    onAwake(): void { 
 
        let showClickBlockCube = WXLogin.getUrlParam("showClickBlockCube");
        let jumpLevel = Number(WXLogin.getUrlParam("jumpLevel"));
        StaticData.showClickBlockCube = showClickBlockCube == "1";
        StaticData.jumpLevel = jumpLevel ? jumpLevel : undefined;
        console.log(" StaticData.jumpLevel", StaticData.jumpLevel);
        
        
        this.BtnLogin.on(Laya.Event.CLICK, this, this.onLoginClick);
        this.EventMask.visible = true;
        this.EventRefish.visible = false;
        /** 资源加载 */
        GameRes.loadRes(() => {
            console.log("资源加载完成");
            WXLogin.WXLogin().then(() => {
                console.log("登录流程成功");
                this.InputName.text = StaticData.nickName;
                if (StaticData.phoneNumber) {
                    this.InputPhone.text = StaticData.phoneNumber;
                }
                this.EventMask.visible = false;
            }).catch(() => {
                console.log("登录流程失败");
                console.log("重新微信登录");
                this.onPop403CommonTips();
            })
        });
        Laya.stage.on(Laya.Event.RESIZE, this, this.onResize);

        Laya.stage.on(EventNames.POPUP_SHOW_COMMON_TIPS, this, this.onPopupShowCommonTips);
        Laya.stage.on(EventNames.POP_403_COMMON_TIPS, this, this.onPop403CommonTips);

    }

    onEnable(): void {
        this.udateViewSize();
    }

    onResize(): void {
        this.udateViewSize();
    }

    onDisable(): void {
        this.udateViewSize();
    }
    
    onDestroy(): void {
        this.BtnLogin.on(Laya.Event.CLICK, this, this.onLoginClick);
        Laya.stage.off(Laya.Event.RESIZE, this, this.onResize);
        Laya.stage.off(EventNames.POPUP_SHOW_COMMON_TIPS, this, this.onPopupShowCommonTips);
        Laya.stage.off(EventNames.POP_403_COMMON_TIPS, this, this.onPop403CommonTips);
    }

    private onLoginClick() {
        Laya.SoundManager.playSound(GameRes.clickAudio, 1);
        let phone = this.InputPhone.text;
        let nickName = this.InputName.text;
        if (!phone || phone.length != 11) {
            console.log("请输入正确的手机号");
            Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, "请输入正确的手机号")
            return;
        }
        if (!nickName) {
            console.log("请输入昵称");
            Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, "请输入昵称")
            return;
        }
        this.login(phone, nickName);
    }

    private login(phone: string, nickName: string) {
        this.EventMask.visible = true;
        HttpManager.post(HttpAPI.PHONE_LOGIN, { phone: phone, name: nickName }).then((res) => {
            console.log("PHONE_LOGIN 登录返回", res);
            if (res.success) {
                console.log("登录成功");
                StaticData.phoneNumber = phone;
                StaticData.nickName = nickName;

                this.gotoHome();
            } else {
                console.log("绑定失败");
                this.EventMask.visible = false;
            }
        }).catch((err) => {
            console.log("绑定失败或网络异常");
            Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, "网络异常请刷新链接")
            this.EventMask.visible = false;
        })
    }

    private gotoHome() {
        Laya.Scene.open("Scene/Home.ls");
    }

    private udateViewSize(){
        let sW = Laya.stage.width;
        let sH = Laya.stage.height;
        let bW = this.ViewBox.width;
        let bH = this.ViewBox.height;
        let scale = Math.max(sW/bW,sH/bH);
        this.ViewBox.scale(scale,scale);
        let maxOffset = 1100
        this.centerX = Math.min(0, (sW / 2) - (maxOffset * scale));
    }

    private onPopupShowCommonTips(str:string){
        const tip = GameRes.getResPrefab(GameRes.PopCommon).create() as PopCommon2
        this.BoxTip, this.addChild(tip)
        tip.centerX = 0;
        tip.y = 200;
        tip.showPop(str,"#1a1a1a")
    }
    private onPop403CommonTips(){
        this.EventRefish.visible = true
        WXLogin.redirect();
    }
}